<?php

namespace app\backstage\controller;

use think\Controller;
use think\Request;
use app\backstage\model\Auth as AuthModel;


class Auth extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request,AuthModel $auth)
    {   

        if($request->isAjax()){
            
            //分页
            $pageSize = 15;
            $params = $request->param();

            $page = $params['page']?:1;
            $offset = ($page-1)*$pageSize;

            $where = [];
            //搜索
            if(!empty($params['start'])){
                $where[] = ['a.create_time','>=',strtotime($params['start'])];
            }

            if(!empty($params['end'])){
                $where[] = ['a.create_time','<=',strtotime($params['end'])];
            }

            if(!empty($params['seach_text'])){
                $where[] = ['a.auth_name','like',"%{$params['seach_text']}%"];
            }
            
            //权限列表
            $data = $auth->alias('a')->join('rbac_auth b','a.pid=b.id','left')
                         ->field('a.id,a.auth_name,a.auth_controller,a.auth_action,a.create_time,a.pid fid,b.auth_name pid,a.is_menu')
                         ->where($where)->order('a.id','desc')
                         ->limit($offset,$pageSize)->select();  
            
            if($data){
                $data = $auth->sortData($data);

                foreach ($data as &$v){
                    $v['create_time'] = date('Y-m-d H:i',$v['create_time']);
                }
            }

            $count = $auth->alias('a')->join('rbac_auth b','a.pid=b.id','left')->where($where)->count();
                
            $pageData = ['count'=>$count,'page'=>(int)$page,'pageSize'=>$pageSize];

            return ['code'=>200,'authData'=>$data,'pageData'=>$pageData];
           
        }

        return view('index');
      
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create(AuthModel $auth)
    {   
        $data = $auth->field('id,auth_name')->order('id','desc')->where('pid',0)->select();

        return view('create',['auth_list'=>$data]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if($request->isAjax()){

            $data = $request->post();

            $auth = new AuthModel($data);

            if(isset($data['namespace'])){
                //一键新增
                //验证参数 
                $check = $auth->valiData2($data);
                if($check['code'] !=200) return $check;
                
                //插入多条数据
                $res = $auth->createList($data);
              

            }else{
                //验证参数 
                $check = $auth->valiData($data);
                
                if($check['code'] !=200) return $check;
    
                //插入数据
                $auth->create_time = time();
                $res = $auth->allowField(true)->save();
            }

           
           if($res){
               return ['code'=>200,'res'=>$res];
           }else{
               return ['code'=>101,'msg'=>'插入失败'];
           }

        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {   

        $info = AuthModel::where('id',$id)->find();
        $data = AuthModel::field('id,auth_name')->order('id','desc')->where('pid',0)->select();
        
        return view('edit',['auth_list'=>$data,'info'=>$info]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        if($request->isAjax()){
        
            $data = $request->post();

            $auth = new AuthModel();

            //验证参数 
            $check = $auth->valiData($data);

            if($check['code'] !=200) return $check;

            //修改数据
            $res = $auth->allowField(true)->save($data,['id'=>$id]);
            if($res){
                return ['code'=>200,'res'=>$res];
            }else{
                return ['code'=>101,'msg'=>'修改失败'];
            }

        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {   
     
        $res = AuthModel::destroy($id);

        if($res){
            return ['code'=>200];
        }else{
            return ['code'=>101,'msg'=>'删除失败'];
        }
    }

    /**
     * 批量删除
     *
     * @param  string  $ids
     * @return \think\Response
     */
    public function delete_all()
    {   

        if($ids = request()->post('ids')){
            $res = AuthModel::destroy($ids);
            
            if($res){
                return ['code'=>200];
            }else{
                return ['code'=>101,'msg'=>'删除失败'];
            }
        }else{
            return ['code'=>101,'msg'=>'缺少参数'];
        }
      
    }
}
